import os

import requests
import json
from 实际调用 import call_interface
import 获取时间区间
from datetime import datetime

now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
url = 'http://59.215.206.188/ddglzy/dispatch/dockingcenter/callpushmonitor/query'
with open('cookie.txt', 'r', encoding='utf-8') as f:
    headers = json.load(f)


data = {
    'bsNum': '',
    'systemName': '',
    'callTime': f'{获取时间区间.last_friday} 至 {获取时间区间.today}',
    'start': 获取时间区间.last_friday,
    'end': 获取时间区间.today,
    'interfaceType': '1',                         #推送申办流水号接口
    'interfaceName': '',
    'callState': '0',
    '_search': 'true',
    'nd': 获取时间区间.millis,
    'rows': '1000',
    'page': '1',
    'sidx': 'CALL_TIME',
    'sord': 'desc'
}
"""
查询所有推送申办流水号接口失败
"""

response = requests.post(url, headers=headers, data=data).json()
# print(response)
for i in response['rows']:
    """
    查询只有一条推送失败接口业务
    """
    from 查询调度情况 import query_dispatch
    re=query_dispatch(i['cell']['BSNUM'] ,获取时间区间.millis)
    # print("打印查询调度情况返回值")
    try:

        print(re)
        for k, v in re.items():
            #判断是否返回空  不为空继续执行
            if not (re is None):
                #打印出 [i['cell']['BSNUM'],i['cell']['CALL_ID'],i['cell']['ITEM_NAME']]
                print(v)
                with open('re0_result.txt', 'r', encoding='utf-8') as f:
                    file_contents = set(f.read().splitlines())
                #判断是否存在调用过的日志里边   在就跳过
                if str(v[0]+':'+v[1]) in file_contents:
                    pass
                else:
                    """
                    实际调用
                    """
                    response = call_interface(v[1])
                    with open('re0_result.txt', 'a', encoding='utf-8') as f:
                        f.write(str(v[0]+':'+str(v[1])) + '\n')
                        #调用成功就打印流水号
                    print(f"\033[92m{v[0],v[3]}\033[0m")
                    import pandas as pd
                    #构建日志格式
                    df = pd.DataFrame({'业务编号': v[0]+':'+v[1],'事项名称':v[2],'调用时间':now,'接口名称':v[3],'调用结果':[response['result']['callState']],'结果': [response['result']['resultValue']]})
                    #判断文件是否存在
                    if os.path.exists('推送结果-4.xlsx'):
                        #读取文件
                        existing_df = pd.read_excel('推送结果-4.xlsx')
                        #拼接数据
                        df = pd.concat([existing_df, df])
                        #最终写入
                    df.to_excel('推送结果-4.xlsx', index=False)
    except:print("\033[91m这个业务有问题  已跳过\033[0m")